// $Id: media.c 94 2011-10-19 12:48:16Z cedric.shih@gmail.com $
/*
 * Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <zcbuffer.h>

#include "log.h"

#include "media.h"

int niortsp_media_describe(struct niortsp_media *media,
		u_int32_t session, const char *uri, struct zcbuffer *buffer)
{
	int rc;
	struct niortsp_track *track;

	niortsp_debug("invoking %p to describe media: %s", media->describe,
			media->name);

	if ((rc = (*media->describe)(media, session, uri, buffer,
			media->cbarg))) {
		niortsp_error("failed to describe media '%s': %s",
				media->name,  strerror(rc));
		goto finally;
	}

	TAILQ_FOREACH(track, &media->tracks, entry) {
		if ((rc = niortsp_track_describe(track, uri, buffer))) {
			niortsp_error("failed to describe track '%s/%s': %s",
					media->name, track->name, strerror(rc));
			goto finally;
		}
	}

	rc = 0;

finally:
	return rc;
}

void niortsp_media_free(struct niortsp_media *media)
{
	if (!media) {
		return;
	}

	niortsp_debug("destructing media: %s", media->name);

	niortsp_track_list_clear(&media->tracks);

	if (media->name) {
		free(media->name);
	}

	free(media);
}

struct niortsp_media *niortsp_media_map_find(struct niortsp_media_map *map,
		const char *name)
{
	struct niortsp_media find;

	niortsp_debug("finding media: %s", name);

	find.name = (char *)name;

	return RB_FIND(niortsp_media_map, map, &find);
}

int niortsp_media_map_add(struct niortsp_media_map *map, const char *name,
		struct niortsp_media **ptr)
{
	int rc;
	struct niortsp_media *media;

	if (niortsp_media_map_find(map, name)) {
		rc = EEXIST;
		niortsp_error("existed: %s", name);
		goto finally;
	}

	niortsp_debug("adding media: %s", name);

	if (!(media = (struct niortsp_media *)calloc(1, sizeof(*media)))) {
		rc = errno ? errno : -1;
		niortsp_error("calloc: %s", strerror(rc));
		goto finally;
	}

	TAILQ_INIT(&media->tracks);

	if (!(media->name = strdup(name))) {
		rc = errno ? errno : -1;
		niortsp_error("strdup: %s", strerror(rc));
		goto finally;
	}

	RB_INSERT(niortsp_media_map, map, media);
	*ptr = media;
	rc = 0;

finally:
	return rc;
}

void niortsp_media_map_remove(struct niortsp_media_map *map,
		struct niortsp_media *media)
{
	niortsp_debug("removing media: %p", media);

	RB_REMOVE(niortsp_media_map, map, media);
	niortsp_media_free(media);
}

void niortsp_media_map_clear(struct niortsp_media_map *map)
{
	struct niortsp_media *media;

	while ((media = RB_MIN(niortsp_media_map, map))) {
		niortsp_media_map_remove(map, media);
	}
}

int niortsp_media_compare(struct niortsp_media *a, struct niortsp_media *b)
{
	return strcmp(a->name, b->name);
}

RB_GENERATE(niortsp_media_map, niortsp_media, entry, niortsp_media_compare);
